<template>
  <el-dialog
    :title="
      !dataForm.categoryId
        ? this.$i18n.t('brand.add')
        : this.$i18n.t('brand.revise')
    "
    :close-on-click-modal="false"
    @close="handleClose"
    :visible.sync="visible"
    width="40%"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="150px"
      :inline="true"
    >
      <el-form-item
        v-if="dataForm.type !== 2 && isSecondLevel === false"
        :label="this.$i18n.t('category.categoryPicture')"
        prop="pic"
      >
        <img-upload v-model="dataForm.pic"></img-upload>
        <span v-if="dataForm.parentId === 0"
          >{{ $t("category.recPicSize") }}532*160</span
        >
        <span v-if="dataForm.parentId !== 0"
          >{{ $t("category.recPicSize") }}120*120</span
        >
      </el-form-item>
      <el-form-item
        v-if="dataForm.parentId === 0"
        :label="this.$i18n.t('category.categoryIcon')"
        prop="icon"
      >
        <img-upload v-model="dataForm.icon"></img-upload>
      </el-form-item>
      <el-form-item
        v-if="dataForm.type !== 2"
        :label="this.$i18n.t('publics.categoryCn')"
        prop="categoryName"
      >
        <el-input
          v-model.trim="dataForm.categoryName"
          controls-position="right"
          :min="0"
          :label="this.$i18n.t('publics.categoryCn')"
          maxlength="7"
          show-word-limit
          style="width: 400px"
        ></el-input>
      </el-form-item>
      <!-- <el-form-item
        v-if="dataForm.type !== 2"
        :label="this.$i18n.t('publics.categoryEn')"
        prop="categoryNameEn"
      >
        <el-input
          v-model="dataForm.categoryNameEn"
          controls-position="right"
          :min="0"
          maxlength="50"
          show-word-limit
          :label="this.$i18n.t('publics.categoryEn')"
          style="width: 400px"
        ></el-input>
      </el-form-item> -->
      <!-- <el-form-item label="上级分类">
        <el-cascader
          expand-trigger="hover"
          :options="categoryList"
          :props="categoryTreeProps"
          :clearable="true"
          v-model="selectedCategory"
          @change="handleChange"
        ></el-cascader>
      </el-form-item>-->
      <el-form-item :label="this.$i18n.t('category.categoryParent')">
        <el-cascader
          expand-trigger="hover"
          :options="categoryList"
          :props="categoryTreeProps"
          :clearable="true"
          v-model="selectedCategory"
          @change="handleChange"
          style="width: 400px"
        ></el-cascader>
      </el-form-item>
      <el-form-item
        v-if="dataForm.type !== 2"
        :label="this.$i18n.t('hotSearch.seq')"
        prop="seq"
      >
        <el-input-number
          style="width: 400px"
          v-model="dataForm.seq"
          controls-position="right"
          :min="0"
          :max="100000000"
          :label="this.$i18n.t('hotSearch.seq')"
        ></el-input-number>
      </el-form-item>
      <el-form-item
        :label="this.$i18n.t('product.status')"
        size="mini"
        prop="status"
      >
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">{{ $t("brand.offline") }}</el-radio>
          <el-radio :label="1">{{ $t("brand.normal") }}</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div
      slot="footer"
      class="dialog-footer"
      style="text-align: left; margin-left: 150px; padding: 0 0 30px"
    >
      <el-button size="small" @click="handleClose">{{
        $t("remindPop.cancel")
      }}</el-button>
      <el-button size="small" type="primary" @click="dataFormSubmit()">{{
        $t("remindPop.confirm")
      }}</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { treeDataTranslate, idList } from "@/utils";
import ImgUpload from "@/components/img-upload";
// import PicUpload from '@/components/pic-upload'
export default {
  data() {
    return {
      visible: false,
      isSecondLevel: false,
      dataForm: {
        categoryId: 0,
        grade: 0,
        categoryName: "",
        categoryNameEn: "",
        seq: 1,
        status: 1,
        parentId: 0,
        pic: "",
        icon: "",
      },
      isSubmit: false,
      dataRule: {
        categoryName: [
          {
            required: true,
            message: this.$i18n.t("publics.categoryNoNull"),
            trigger: "blur",
          },
        ],
        categoryNameEn: [
          {
            required: true,
            message: this.$i18n.t("publics.categoryNoNull"),
            trigger: "blur",
          },
        ],
        pic: [
          {
            required: true,
            message: this.$i18n.t("category.categoryPicNull"),
            trigger: "blur",
          },
        ],
        icon: [
          {
            required: true,
            message: this.$i18n.t("category.categoryIconNull"),
            trigger: "blur",
          },
        ],
      },
      categoryList: [],
      selectedCategory: [],
      categoryTreeProps: {
        value: "categoryId",
        label: "categoryName",
        checkStrictly: true,
      },
    };
  },
  watch: {
    dataForm: {
      handler() {
        if (this.dataForm.type !== 2 && this.isSecondLevel) {
          this.dataForm.pic = "";
        }
        if (this.dataForm.parentId !== 0) {
          this.dataForm.icon = "";
        }
      },
      deep: true, // true 深度监听
    },
  },
  components: {
    ImgUpload,
  },
  methods: {
    init(id) {
      console.log(id);
      this.isSecondLevel = false;
      this.dataForm.parentId = 0;
      this.dataForm.categoryId = id || 0;
      this.dataForm.icon = "";
      this.isSubmit = false;
      this.$http({
        url: this.$http.adornUrl(
          "/prod/category/upAndCurrCategoryList/" + this.dataForm.categoryId
        ),
        method: "get",
        params: this.$http.adornParams({
          maxGrade: 1,
          status: 1,
        }),
      })
        .then(({ data }) => {
          this.categoryList = treeDataTranslate(data, "categoryId", "parentId");
        })
        .then(() => {
          this.visible = true;
          this.$nextTick(() => {
            this.$refs["dataForm"].resetFields();
            this.selectedCategory = [];
          });
        })
        .then(() => {
          if (this.dataForm.categoryId) {
            // 修改
            this.$http({
              url: this.$http.adornUrl(
                `/prod/category/info/${this.dataForm.categoryId}`
              ),
              method: "get",
              params: this.$http.adornParams(),
            }).then(({ data }) => {
              this.isSecondLevel = data.grade === 1;
              this.dataForm.categoryId = data.categoryId;
              this.dataForm.categoryName = data.categoryName;
              this.dataForm.categoryNameEn = data.categoryNameEn;
              this.dataForm.seq = data.seq;
              this.dataForm.pic = data.pic;
              this.dataForm.icon = data.icon;
              this.dataForm.parentId = data.parentId;
              this.dataForm.status = data.status;
              this.selectedCategory = idList(
                this.categoryList,
                data.parentId,
                "categoryId",
                "children"
              ).reverse();
            });
          }
        });
    },
    handleClose() {
      this.dataForm = {
        categoryId: 0,
        grade: 0,
        categoryName: "",
        categoryNameEn: "",
        seq: 1,
        status: 1,
        parentId: 0,
        pic: "",
        icon: "",
      };
      this.visible = false;
    },

    handleChange(val) {
      this.isSecondLevel = val.length === 1;
      this.dataForm.parentId = val[val.length - 1];
      if (!this.dataForm.parentId && this.dataForm.parentId !== 0) {
        this.dataForm.parentId = 0;
      }
    },
    // 表单提交
    dataFormSubmit() {
      if (this.selectedCategory.length === 1) {
        this.dataForm.grade = 0;
      }
      if (this.selectedCategory.length === 2) {
        this.dataForm.grade = 1;
      }
      if (this.selectedCategory.length === 3) {
        this.dataForm.grade = 2;
      }
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          if (this.isSubmit) {
            return false;
          }
          this.isSubmit = true;
          this.$http({
            url: this.$http.adornUrl(`/prod/category`),
            method: this.dataForm.categoryId ? "put" : "post",
            data: this.$http.adornData({
              categoryId: this.dataForm.categoryId || undefined,
              categoryName: this.dataForm.categoryName,
              categoryNameEn: this.dataForm.categoryNameEn,
              status: this.dataForm.status,
              seq: this.dataForm.seq,
              grade: this.dataForm.grade,
              parentId: this.dataForm.parentId,
              pic: this.dataForm.pic,
              icon: this.dataForm.parentId === 0 ? this.dataForm.icon : null,
            }),
          })
            .then(({ data }) => {
              this.$message({
                message: this.$i18n.t("remindPop.success"),
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                  setTimeout(() => {
                    this.dataForm = {
                      categoryId: 0,
                      grade: 0,
                      categoryName: "",
                      categoryNameEn: "",
                      seq: 1,
                      status: 1,
                      parentId: 0,
                      pic: "",
                      icon: "",
                    };
                  }, 1500);
                },
              });
            })
            .catch((e) => {
              this.isSubmit = false;
            });
        }
      });
    },
  },
};
</script>
